//生成菜单
var menuItem = Vue.extend({
    name: 'menu-item',
    props: {item: {}},
    template: [
        '<li>',
        '<a v-if="item.type === 0" href="javascript:;">',
        '<i v-if="item.icon != null" :class="item.icon"></i>',
        '<span>{{item.name}}</span>',
        '<i class="fa fa-angle-left pull-right"></i>',
        '</a>',
        '<ul v-if="item.type === 0" class="treeview-menu">',
        '<menu-item :item="item" v-for="item in item.subMenuList"></menu-item>',
        '</ul>',
        '<a v-if="item.type === 1" :href="\'#\'+item.url"><i v-if="item.icon != null" :class="item.icon"></i><i v-else class="fa fa-circle-o"></i> {{item.name}}</a>',
        '</li>'
    ].join('')
});

//iframe自适应
$(window).on('resize', function () {
    var $content = $('.content');
    $content.height($(this).height() - 120);
    $content.find('iframe').each(function () {
        $(this).height($content.height());
    });
}).resize();

//注册菜单组件
Vue.component('menuItem', menuItem);

var vm = new Vue({
    el: '#rrapp',
    data: {
        user: {},
        menuList: {},
        main: "sys/main.html",
        password: '',
        newPassword: '',
        navTitle: "控制台",
    },
    methods: {
        getMenuList: function (event) {
            $.getJSON("sys/menu/user?_" + $.now(), function (r) {
                vm.menuList = r.jsonResult;
                // window.permissions = r.permissions;
                // $.cookie('permissions', JSON.stringify(r.permissions));
            });
        },
        getUser: function () {
            $.getJSON("sys/user/info?_" + $.now(), function (r) {
                vm.user = r.jsonResult;
            });
        },
        updatePassword: function () {
            vm.getUser();
            layer.open({
                type: 1,
                skin: 'dialog-class',
                title: "修改密码",
                area: ['550px', '350px'],
                shadeClose: false,
                content: jQuery("#passwordLayer"),
                btn: ['修改', '取消'],
                btn1: function (index) {
                    if (vm.newPassword === '' && vm.password === '') {
                        //不填密码
                    } else if (vm.newPassword.length < 6 || !(/^[a-zA-Z\d]+$/.test(vm.newPassword))) {
                        parent.layer.alert('至少包含字母和数字，最短6位字符');
                        return;
                    }
                    var data = "password=" + vm.password + "&newPassword=" + vm.newPassword + "&remark=" + vm.user.remark;
                    $.ajax({
                        type: "POST",
                        url: "sys/user/password",
                        data: data,
                        dataType: "json",
                        success: function (result) {
                            if (result.code == 0) {
                                layer.close(index);
                                layer.alert('修改成功', function (index) {
                                    location.reload();
                                });
                            } else {
                                layer.alert(result.msg);
                            }
                        }
                    });
                }
            });
        }
    },
    created: function () {
        this.getMenuList();
        this.getUser();
    },
    updated: function () {
        //路由
        var router = new Router();
        routerList(router, vm.menuList);
        router.start();
    }

});


function routerList(router, menuList) {
    for (var key in menuList) {
        var menu = menuList[key];
        if (menu.type == 0) {
            routerList(router, menu.subMenuList);
        } else if (menu.type == 1) {
            router.add('#' + menu.url, function () {
                var url = window.location.hash;
                vm.main = url.replace('#', '');
                //替换iframe的url
                // $("#myIframe").attr('src',vm.main);
                //删除iframe 防止记录history
                $("#myIframe").remove();
                $("#content").append('<iframe id="myIframe" scrolling="yes" frameborder="0" style="width:100%;min-height:200px;height:100%;hoverflow:visible;background:#fff;" :src="main"></iframe>');
                //导航菜单展开
                $(".treeview-menu li").removeClass("active");
                $("a[href='" + url + "']").parents("li").addClass("active");

                var parentTitle = $("a[href='" + url + "']").parent('li').parent('ul').parent('li').children('a').text();
                vm.navTitle = parentTitle + " > " + $("a[href='" + url + "']").text();
            });
        }
    }
}

